package com.lincoln.redis.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lincoln.redis.server.StaticValue;
import com.lincoln.redis.server.config.PermissionConfig;
import com.lincoln.redis.server.entity.User;
import com.lincoln.redis.server.service.UserService;
import com.lincoln.redis.server.utils.SpringBeanFactoryUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户数据操作
 *
 * @author lincoln
 */
@Component
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    @Override
    public String exTableKeyHeader() {
        return StaticValue.SYSTEM_REDIS_TABLE_USER;
    }

    @Override
    public String exGenerateKey(User user) throws Exception {
        if (user == null) {
            return exTableKeyHeader() + ".*";
        }
        if (user.getId() == null && user.getUsername() == null) {
            throw new Exception("key生成失败!");
        }
        if ("".equals(user.getId()) || "".equals(user.getUsername())) {
            throw new Exception("key生成失败!");
        }
        return exTableKeyHeader() + "." + (user.getId() == null ? "*" : user.getId()) + "_" + (user.getUsername() == null ? "*" : user.getUsername());
    }

    @Override
    public boolean save(User user) {
        if (user.getId() == null && findByName(user.getUsername()) != null) {
            return false;
        }
        if (user.getPermissions() == null || user.getPermissions().size() <= 0) {
            Set<String> permissions = new HashSet<>();
            //每个用户都有的权限
            for (String base : SpringBeanFactoryUtils.getBean(PermissionConfig.class).getBase()) {
                permissions.add(base);
            }
            user.setPermissions(permissions);
        }
        return super.save(user);
    }

    @Override
    public User findByName(String username) {
        User u = new User();
        u.setUsername(username);
        Set<String> keys = null;
        try {
            keys = jedis.keys(exGenerateKey(u));
        } catch (Exception e) {
            return null;
        }
        if (keys == null || keys.size() <= 0) {
            return null;
        }
        String json = jedis.get(keys.iterator().next());
        User user = JSONObject.parseObject(json, User.class);
        return user;
    }

    @Override
    public List<User> findAll(String username) {
        Set<String> keys = null;
        try {
            keys = jedis.keys(exTableKeyHeader() + ".*_*" + username + "*");
        } catch (Exception e) {
            return new ArrayList<>();
        }
        if (keys == null || keys.size() <= 0) {
            return new ArrayList<>();
        }
        List<User> list = new ArrayList<>();
        for (String key : keys) {
            list.add(JSONObject.parseObject(jedis.get(key), User.class));
        }
        return list;
    }
}
